package com.it.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

// 基于插件的延迟消息发送
@Configuration
public class DelayedMsgConfig {
    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    // 基于插件的交换机
    /*String name,
     String type,
     boolean durable,
     boolean autoDelete,
     Map<String, Object> arguments*/
    @Bean
    public Exchange delayedExchange(){
        Map<String, Object> arguments = new HashMap<>();
        // x-delayed-type 交换机的类型
        // x-delayed-message 延迟消息的类型
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(exchange_delay,"x-delayed-message",true,false,arguments);
    }

    // 基于插件的队列
    @Bean
    public Queue delayedQueue(){
        return QueueBuilder.durable(queue_delay_1).build();
    }

    // 绑定队列和交换机
    @Bean
    public Binding bindDelayedExchangeAndQueue(
            @Qualifier(value = "delayedQueue") Queue delayedQueue,
            @Qualifier(value = "delayedExchange") Exchange delayedExchange
    ){
        // 绑定哪个队列到哪个交换机上
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(routing_delay).noargs();
    }
}
